package com.mall.utils;

import com.cloopen.rest.sdk.CCPRestSmsSDK;

import java.util.*;

/**
 * 短信发送工具类
 */
public class SmsUtil {

    private  static final String serverIp="app.cloopen.com";
    private  static final String serverPort="8883";
    private  static final String accountId="8a216da87ba59937017be70e95f60e61";
    private  static final String accountToken="e39159ac617e42099222f3d332cc8fe6";
    private  static final String appId= "8a216da87ba59937017be70e96c40e67";

    public static   List<Map<String,Object>> sendSmsToMobile(List<String>mobiles,String templateId,String []datas){

        List<Map<String,Object>> list=new ArrayList<>();

        Map<String,Object> result=null;//用于接收短信发送结果
        CCPRestSmsSDK api=new CCPRestSmsSDK();
        api.init(serverIp,serverPort);
        api.setAccount(accountId,accountToken);
        api.setAppId(appId);

        for (String to:mobiles) {
            Map<String,Object>map=new HashMap<>();
            //执行发送
            result = api.sendTemplateSMS(to, "1", datas);
            String code=result.get("statusCode").toString();
            String msg="";
            if("000000".equals(code)){
                System.out.println("发送信息："+result.get("data"));
                msg=String.format("信息码%s，信息：%s",code,result.get("statusMsg"));
                map.put("is",true);

            }else {
                msg=String.format("错误码%s，错误信息：%s",code,result.get("statusMsg"));
                System.out.println(msg);
                map.put("is",false);

            }
            map.put("time",new Date().getTime());
            map.put(to,msg);
           list.add(map);
        }
            return list;

    }

    public static void main(String[] args) {
//        String mobiles[]={"13907844365"};
//        String datas[]={"74258","5"};
//
//       List<Map<String,Object>>list =sendSmsToMobile(mobiles,"1",datas);
//        for (Map<String,Object>map:list) {
//            System.out.println(map.toString());
//        }
    }

}
